<?php
class DefaultListAction extends FrontendAction
{
	public function __construct()
	{
		parent::__construct();
		$this->layout = 'index';
	}
	
	public function execute()
	{
		//四种查询方式
		//1. 按类目查询
		//2. 按标签查询
		//3. 按商品ID查询
		//4. 全部搜索
		$categoryId = trim($this->context->request('c'));
		if(!is_numeric($categoryId))
		{
			$categoryId = '';
		}
		$tag = trim($this->context->request('t'));
		if(!empty($tag))
		{
			//从tag中解析商品链接
			$id = $this->parseItemUrl($tag);
			if(empty($id))
			{
				//编码转换
				$tag = $this->decodeKeyword($tag);
			}
		}
		
		
		//拼装查询条件
		$con = array();
		if(is_numeric($id))
		{
			$con['ids'] = $id; 
		}
		else if(!empty($tag))
		{
			$con['tag'] = $tag;
		}
		else if(is_numeric($categoryId))
		{
			$category = C('CategoryService')->get($categoryId);
			if($category)
			{
				$con['tag'] = $category['tags'];
			}
		}
		
		$page = $this->getPage();
		$pageSize = 20;
		
		//搜索分享
		$shareService = new ShareService('share');
//		$result = $shareService->query($con, $page, $pageSize);
//		$totalNum = $result['totalNum'];
//		$this->data['columns'] = $result['list'];
//		$ids = Util::parseIds('id', $this->data['columns']);
		
		$imgs = array(2,7,8,9,10,11,12);
		$item = array(
			'pic'=>'',
			'share_num'=>50,
			'collect_num'=>30,
			'title'=>'【yuki】头层牛皮欧美小香家菱格短靴厚底短靴真皮鞋',
			'price'=>150,
			
			'username'=>'周杨华',
			'logo'=>'/images/1.jpg',
			'created'=>'1个月前',
				
			'share_id'=>2,
			'share_content'=>'实物很好看哦，买的是单层内里，皮质也不错，软软的，好喜欢好喜欢\(^o^)/~穿上脚很舒服，我平时穿37码的鞋，这款我也选37，我的脚比较... ',
		);
		
		$this->data['columns'] = array();
		$ids = array();
		$id = 1;
		for($j=0;$j<20;$j++){
			$ids[] = $id;
			$item['pic'] = '/images/'.$imgs[array_rand($imgs, 1)].'.jpg';
			$item['collect_num'] = 0;
			$item['share_id'] = $id++;
			$this->data['columns'][] = $item;
		}
		
		if ($ids)
		{
			$tmp = $shareService->rows(array('id'=>array('in'=>$ids)), '', array('id','collect_num'));
			$tmp = Util::groupData($tmp);
			
			foreach($this->data['columns'] as &$share)
			{
				if(array_key_exists($item['id'], $tmp))
				{
					$share['collect_num'] = $result[$item['id']]['collect_num'];
				}
				else
				{
					$share['collect_num'] = 0;
				}
			}
		}
		
		$ltConfig = C('LtConfig');
		$pagerConfig = $ltConfig->get('pagination.pager');
		if($this->data['isRobot'])
		{
			$pagerConfig['num_links_show'] = 100;
		}
		else
		{
			$pagerConfig['num_links_show'] = 10;
		}
		$pagerConfig['per_page'] = $pageSize;
		
		
		$params = array('c'=>$categoryId,'t'=>$tag);
		$params['p'] = ':page';
		$baseUrl = C('LtUrl')->generate('Default', 'List', $params);
		$pagination = new PiscesPagination;
		$pagination->init($pagerConfig);
		$this->data['pages'] = $pagination->pager($page, $totalNum, $baseUrl);
		
		//seo
		$seo = Util::loadSetting('seo');
		$this->data['seo']['title']  = $this->getSeoStr($seo['listtitle'], array('cat'=>$category['name'],'tag'=>$con['tag'],'page'=>$page));
		$this->data['seo']['keyword']  = $this->getSeoStr($seo['listkeyword'], array('cat'=>$category['name'],'tag'=>$con['tag'],'page'=>$page));
		$this->data['seo']['description']  = $this->getSeoStr($seo['listdescription'], array('cat'=>$category['name'],'tag'=>$con['tag'],'page'=>$page));
	}
	
	private function decodeKeyword($keyword)
	{
		if(!function_exists('mb_detect_encoding'))
		{
			return $keyword;
		}
		
		$defaultEncode = $this->data['site']['encode'];
		if(empty($defaultEncode) || $defaultEncode!='UTF-8')
		{
			return $keyword;
		}
		
		$encode = mb_detect_encoding($keyword, Util::getDefaultArray($defaultEncode,array('EUC-CN','ASCII','UTF-8','GB2312','GBK','BIG5')));
		if($encode!='UTF-8')
		{
			$keyword = iconv($encode,'UTF-8',$keyword);
		}
		
		$keyword = htmlspecialchars(trim($keyword));
		
		return $keyword;
	}
	
	private function parseItemUrl($keyword)
	{
		$productId = '';
		$tmp = parse_url($keyword);
		$query = $tmp['query'];
		if(preg_match('~id=([^&]*)~',$query,$m))
		{
			$productId	= $m[1];
		}
		
		if(!is_numeric($productId))
		{
			return false;			
		}
		
		return $productId;
	}
}
